<?php

function amankan($str) {
	// experimental
	if (get_magic_quotes_gpc()) {
		return $str;
	} else {
		if (function_exists('addslashes')) {
			return addslashes($str);
		} else {
			return mysql_real_escape_string($str);
		}
	}
}

function open_db() {
	$host = 'localhost';
	$user = 'root';
	$pass = 'bismillah';
	$dbname = 'realita';
	error_reporting(E_ALL ^ E_WARNING); // remove this on release
	$con = mysql_connect($host,$user,$pass,true) or die(view_error('Database Error',mysql_error()));
	mysql_select_db($dbname,$con) or die(view_error('Database Error',mysql_error()));
	error_reporting(E_ALL); // remove this on release
	return $con;
}

function close_db(&$con) {
	mysql_close($con);
}

function db_result($query) {
	$retval = false;
	$result = mysql_query($query);
	if ($result && (mysql_num_rows($result) > 0)) {
		while ($row = mysql_fetch_assoc($result)) {
			$retval = $row;
			break;
		}
	}
	mysql_free_result($result);
	return $retval;
}

function db_array($query) {
	$retval = array();
	$result = mysql_query($query);
	if ($result && (mysql_num_rows($result) > 0)) {
		while ($row = mysql_fetch_assoc($result)) {
			$retval[] = $row;
		}
	}
	mysql_free_result($result);
	return $retval;
}

function db_insert($query) {
	$result = mysql_query($query);
	if ($result == false) {
		$retval = false;
	} else {
		$retval = mysql_insert_id();
	}
	return $retval;
}

function db_query($query) {
	$retval = mysql_query($query);
	return $retval;
}
